<?php
class FileuploadsController extends AppController {
	//Khai bao name cua controller, ten controller phai tuong ung voi ten cua view.
	// luu y: "$this->params['controller']" duoc su dung de lay ten cua controller khi viet code o cac trang view
	public $name = 'Fileuploads';
	//khai bao cac model can su dung trong controller
	//luu y: "$this->uses[0]" duoc su dung de lay ten cua cac model khi viet code o controller va`
	//        "$this->params['models'][0]" duoc su dung de lay ten cua model khi viet code o cac trang view
	public $uses=array('FileManager','Image','ProductImage','FriendlyUrl');
	//khai bao cac helper de su dung khi viet code o view, lu y chi can khai bao ten, ko can Helper o phia sau, vd CommonHelper -> Common
	//cac helper mac dinh da co la SessionHelper, FormHelper, HtmlHelper....
	//khi goi cac helper o ngoai view thi goi nhu sau:  SessionHelper -> $this->Session, FormHelper -> $form, CommonHelper-> $this->Common
	public $components=array('ImageUtil','Common');
	
	function beforeFilter(){
		
		 if (isset($this->data['PHPSESSID'])) {
            $this->Session->id($this->data['PHPSESSID']);
           // $this->Session->start();            
        }       
        
        parent::beforeFilter();
	}
	function admin_imageupload($type=null,$model=null,$galleryId=null){	
		// list of valid extensions, ex. array("jpeg", "xml", "bmp")
		$allowedExtensions = array();
		// max file size in bytes
		$sizeLimit = 10 * 1024 * 1024;
		App::uses('qqFileUploader', 'Lib');
		$uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
		$fileName=$uploader->getName();
		//wipe out .jpg.jpg			
		$fileNameWOExt=substr($fileName,0, strrpos($fileName,'.'));
		$fileExt=substr($fileName,strrpos($fileName,'.'));		
		if(!Configure::read('ImageOverwrite')){
            /// don't overwrite previous files that were uploaded
			$data=$this->FileManager->find('first',array('conditions'=>array('model'=>$model,'type'=>$type,'name'=>$fileNameWOExt.$fileExt)));
			while (!empty($data)) {
				$fileNameWOExt .= rand(10, 99);
				$data=$this->FileManager->find('first',array('conditions'=>array('model'=>$model,'type'=>$type,'name'=>$fileNameWOExt.$fileExt)));	            
	        }
		}
		$fileName=$fileNameWOExt.$fileExt;
		
		if (!empty($fileName) && $type!=null){

				if($type=="document"){		
					$uploadImage='uploaded/'.$model.'/document/';
					$this->Common->createWebRootFolder($uploadImage);	
					
					//save image	
					$uploader->handleUpload($uploadImage,$fileName);			
					//move_uploaded_file($this->params['form']['Filedata']['tmp_name'], $uploadImage);
					echo '{"FILEID":"' . $fileName.'","success":true}';	// Return the file id to the script
				}
				else if($type=="image"){
					$uploadImage='uploaded/'.$model.'/image/';
					$this->Common->createWebRootFolder($uploadImage);				
					
					//save image			
					//move_uploaded_file($this->params['form']['Filedata']['tmp_name'], $uploadImage);
					$uploader->handleUpload($uploadImage,$fileName,true);

					
					$Image='uploaded/'.$model.'/thumb/';
					$this->Common->createWebRootFolder($Image);	
					
					$height=90;
					$width=120;
					//get size config
					if($thumbConfigSize=Configure::read($model.'.thumb')){
						$height=$thumbConfigSize['height'];				
						$width=$thumbConfigSize['width'];;
					}
					$this->ImageUtil->convertImageSize($uploadImage.$fileName,$Image.$fileName,$width,$height);
					//thumb2 image (thumb2) ... the same as thumb1 but change the size ($thumbwidth and $thumbheight)
					echo '{"FILEID":"' . $fileName.'","success":true}';	// Return the file id to the script
				}
				
				//save to db 
				$galleryId=intval($galleryId);
				if($galleryId){
					$this->FileManager->save(array('FileManager'=>array('model'=>$model,'type'=>$type,'name'=>$fileName,'mount'=>1)));
					
					$this->{$model}->save(array($model=>array('image'=>$fileName,'image_category_id'=>$galleryId)));
					if($model=='Image'){
						//================seo link add
						$lastinsertId=$this->{$model}->getLastInsertID();
						$title='Untitle Image';
						$this->updateSeoLink('/lifes/detail/'.$lastinsertId.'/'.$this->Session->read('adminLang'), $title, $lastinsertId);
						//update view_order for gallery
						$this->{$model}->updateAll(array('view_order'=>$lastinsertId),array($model.'.id'=>$lastinsertId));
						//=======================
					}
					
				}
				else $this->FileManager->save(array('FileManager'=>array('model'=>$model,'type'=>$type,'name'=>$fileName)));
			
		}
		exit;
	}	
	

	

}
?>
